package 我的Java学习_算法基础.day_03;

import java.util.Arrays;


public class _044_合并有序数组 {
    public static void main(String[] args) {
        int[] a ={2,3,5,7,9,0,0,0,0,0};
        int[] b ={1,4,6,8,10};
        marge(a,b);
        System.out.println(Arrays.toString(a));
    }
    private static void marge(int[] a,int[] b){
        int l = a.length ;
        int ar = a.length-b.length-1, br = b.length-1;//最右边有效数下标
        int current = l-1;
        while(ar>=0&&br>=0){
            if(a[ar]>=b[br]){   //大的数放在最右边
                a[current] = a[ar];
                ar--;
            } else{
                a[current]=b[br];
                br--;
            }
            current--;

        }
        while(br>-1){           //b中还有元素没排，依次放到a里；a中有元素无需考虑
            a[current] = b[br];
            current--;
            br--;
        }
    }
}
